<template>
  <div style="min-width: 1100px">
    <div class="licc-order-return-applay-top">
      <el-form :model="filterOrderList" ref="filterOrderList"  label-width="80px">
        <el-row>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="orderSn"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.orderSn" placeholder="请输入订单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="phone"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.phone" placeholder="退货人手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请时间:">
              <el-date-picker
                v-model="datePickerValue"
                value-format="yyyy-MM-dd HH:mm:SS"
                :picker-options="pickerOptions"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="2">
            <el-button type="info" @click="restOrderInfosButton">重 置</el-button>
            <el-button type="primary" @click="selectOrderInfosButton">筛 选</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-table
        stripe
        border
        :data="orderInfos"
        :default-sort = "{prop: 'createDate', order: 'descending'}">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column prop="orderSn" label="订单编号" sortable></el-table-column>
        <el-table-column prop="invoiceNo" label="发货单号" sortable></el-table-column>
        <el-table-column prop="applayReturnTime" label="申请时间"  sortable></el-table-column>
        <el-table-column prop="orderStatus" label="状态">
          <el-tag type="warning">申请退货</el-tag>
        </el-table-column>
        <el-table-column prop="orderReturn.returnNote" label="退货原因"></el-table-column>
        <el-table-column prop="shopUser.phone" label="退货人手机号"></el-table-column>
        <el-table-column prop="goodsAmount" label="金额（元）" ></el-table-column>
        <el-table-column prop="phone" label="收货人手机号"></el-table-column>
        <el-table-column label="收货人地址">
          <template slot-scope="scope">
            {{initAddressInfo(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column prop="postscript" label="订单附言"></el-table-column>
        <el-table-column prop="payTime" label="支付时间"></el-table-column>
        <el-table-column prop="deliveryTime" label="收货时间"></el-table-column>
        <el-table-column fixed="right" width="185" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-tickets"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleInfo(scope.$index, scope.row)">详情</el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleEdit(scope.$index, scope.row)">处理</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 35, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodsTotal">
        </el-pagination>
      </div>
    </div>

    <!-- 详细信息 -->
    <div>
      <el-dialog :title="orderInfoDialogTitle" :visible.sync="dialogOrderInfoVisible" width="60%">
        <span>
          <order-info v-if="dialogOrderInfoVisible" :message="orderAllInfo"></order-info>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogOrderInfoVisible = false">返 回</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        :title="orderReturnInfoDialogTitle"
        :visible.sync="dialogOrderReturnVisible"
        width="50%"
        :before-close="orderReturnClose">
        <el-form :model="orderReturnInfo" :rules="orderReturnInfoRules" ref="orderReturnInfo" class="licc-order-deliver-info-form" label-width="95px">
          <el-form-item label="退货原由：" prop="returnNote">
            <el-input type="textarea" name="returnNote" v-model="orderReturnInfo.returnNote" placeholder="退货原由不明确" disabled></el-input>
          </el-form-item>
          <el-form-item label="处理结果：" prop="processInformation">
            <el-input type="textarea" name="processInformation" v-model="orderReturnInfo.processInformation" placeholder="请输入处理结果"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogOrderReturnVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderReturnEditButton(1)">驳 回</el-button>
        <el-button type="primary" @click="orderReturnEditButton(2)">通 过</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import orderApi from '@/api/backstage/orderReturn'
  import OrderInfo from '../components/orderInfo'
  export default {
    name: 'Goods_listInfo',
    components: {
      OrderInfo
    },
    data() {
      const validateProcessInformation = (rule, value, callback) => {
        if (value === ''||typeof(value)=="undefined") {
          callback(new Error('请输入发货单号'));
        }else if (value.length > 120){
          callback(new Error('字数超过120字'));
        }
        callback();
      }
      return {
        /** 筛选条件 **/
        filterOrderList:{
          orderSn:'',
          phone:'',
        },
        /** 日期选择器 **/
        datePickerValue:'',
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        /** 订单信息 **/
        orderInfos:[],
        currentPage: 1,
        pagesize:20,
        goodsTotal:0,
        /** 订单状态 **/
        initStatus:[70],
        /** 详细信息不显示 **/
        dialogOrderInfoVisible: false,
        /** 发货页面 **/
        dialogOrderNextVisible:false,
        /** 订单详细信息 **/
        orderAllInfo:{
          goodsAmount:'',
          shopUser:{phone:''},
          consignee:'',
          phone:'',
          province:'',
        },
        /** 订单详情显示标题 **/
        orderInfoDialogTitle:'',
        /** 退货订单处理标题 **/
        orderReturnInfoDialogTitle: '',
        /** 退货订单处理不显示 **/
        dialogOrderReturnVisible: false,
        /** 退货订单处理页面信息 **/
        orderReturnInfo:{
          returnNote:'',
        },
        orderReturnInfoRules:{
          processInformation:[{required: true, trigger: 'blur', validator: validateProcessInformation}]
        },
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        let params = {
          orderStatusList: this.initStatus,
          currentPage: this.currentPage,
          pagesize: this.pagesize,
          orderSn: this.filterOrderList.orderSn,
          phone: this.filterOrderList.phone,
          createDate:this.datePickerValue
        }
        orderApi.initOrderList(params).then(res => {
          this.orderInfos = res.data.records;
          this.goodsTotal = res.data.total;
        }).catch(() => {
          this.$notify.error({title: '失败', message: "获取用户信息失败"});
        })
      },
      /** 分页---开始 **/
      handleSizeChange(val) {
        this.pagesize = val;
        this.initData();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.initData();
      },
      /** 分页---结束 **/
      /** 详细地址信息 **/
      initAddressInfo(row){
        return row.province+row.city+row.district+row.street+"-"+row.address;
      },
      /** 筛选订单编号数字 **/
      validateOrderSnRule(rule, value, callback){
        let orderSNRule = /^[0-9]*$/;
        if (!orderSNRule.test(value)) {
          console.log(value);
          callback(new Error('只能由数字组成'));
        } else {
          callback()
        }
      },
      /** 重置搜索 **/
      restOrderInfosButton(){
        this.filterOrderList.orderSn = '';
        this.datePickerValue = null;
        this.filterOrderList.orderSn = '';
        this.filterOrderList.phone = '';
        this.$refs['filterOrderList'].clearValidate();
        this.currentPage = 1;
        this.initData();
      },
      /** 条件筛选 **/
      selectOrderInfosButton(){
        this.$refs['filterOrderList'].validate(valid => {
          if (valid){
            this.currentPage = 1;
            this.initData();
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },
      /** 操作按钮点击事件 **/
      handleInfo(orderIndex, row){
        /** 订单详情 **/
        this.orderAllInfo = {};
        this.orderAllInfo = row;
        /** 订单详情标题 **/
        this.orderInfoDialogTitle = "订单号："+row.orderSn+" - 时间："+row.createDate+" - 状态：待发货";
        /** 详细信息显示 **/
        this.dialogOrderInfoVisible = true;
      },
      /** 退货处理按钮 **/
      handleEdit(orderIndex, row){
        this.orderReturnInfoDialogTitle = "订单："+row.orderSn;
        this.orderReturnInfo = row.orderReturn;
        this.$nextTick(function () {
          /** 清楚处理界面样式 **/
          this.$refs['orderReturnInfo'].clearValidate();
        });
        this.dialogOrderReturnVisible = true;
      },
      /** 操作按钮显示 **/
      buttonDisabled(status, type){
      },
      /** 退货订单处理页面处理按钮 type：1-通过； 2-驳回**/
      orderReturnEditButton(type){
        this.$refs['orderReturnInfo'].validate(valid => {
          if (valid){
            let params ={
              id: this.orderReturnInfo.id,
              processInformation: this.orderReturnInfo.processInformation,
              orderId: this.orderReturnInfo.orderId,
              orderStatus: (type==1?95:80)
            };
            orderApi.applyReturnEdit(params).then(res => {
              this.$notify.success({title:'成功',message:'订单'+(type==1?'驳回':'通过')+'成功！'});
              this.dialogOrderReturnVisible = false;
              this.initData();
            }).catch(() => {
              this.$notify.error({title: '失败', message: '订单'+(type==1?'驳回':'通过')+'失败，请稍后再试'});
            })
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },
      /** 处理页面关闭 **/
      orderReturnClose(done) {
        this.$confirm('确认取消操作？').then(_ => {
          done();
        }).catch(_ => {});
      },
    }
  }
</script>

<style>
  .licc-order-return-applay-top{
    width: 100%;
    min-width: 1100px;
  }
  .licc-order-return-applay-form>.el-form-item{
    margin-bottom: 0px;
  }
</style>
